package net.lwf.security;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;


import net.lwf.common.constant.ICommonConstant;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jasypt.util.password.BasicPasswordEncryptor;
import org.jasypt.util.password.PasswordEncryptor;


/**
 * This class will be initialized from the InitializationServlet.
 */
public class LwfSecurity {
	
	//http://www.exampledepot.com/egs/javax.crypto/PassKey.html
	
	private static LwfSecurity instance = null;
	private String transformation;
	private String algorithm;
	private PasswordEncryptor passwordEncryptor = null;
	
	private LwfSecurity() {
	}

	public synchronized final static LwfSecurity getInstance(){
		if (instance == null) {
			instance = new LwfSecurity();
		}
		return instance;
	}

	
	public boolean checkPassword(String actual, String encrypted) {
		return passwordEncryptor.checkPassword(actual, encrypted);
	}
	
	public String encrypt(String source) {
		String encrypted = cipher(source, Cipher.ENCRYPT_MODE);
		return new StringBuffer(new String(encrypted)).reverse().toString();

	}

	public String cipher(String source, int mode) {

		byte[] input = source.getBytes();
		Security.addProvider(new BouncyCastleProvider());
		SecretKeySpec key = new SecretKeySpec(ICommonConstant.keyBytes_DES, this.algorithm);
		Cipher cipher = null;
		byte[] cipherText = null;
		try {
			cipher = Cipher.getInstance(this.transformation, ICommonConstant.Provider_BC);
			cipher.init(mode, key);
			cipherText = cipher.update(input);
			cipherText = cipher.doFinal(input);
		} catch (NoSuchAlgorithmException e) {

		} catch (NoSuchProviderException e) {

		} catch (NoSuchPaddingException e) {
			
		} catch (IllegalBlockSizeException e) {

		} catch (BadPaddingException bpex) {
			bpex.printStackTrace();
		} catch (InvalidKeyException e) {

		}

		return new String(cipherText);
	}

	public String decrypt(String source) {
		String encrypted = new StringBuffer(new String(source)).reverse()
				.toString();
		return cipher(encrypted, Cipher.DECRYPT_MODE);
	}
	
	//----------------------------------------------------------------------------------------------
	// Initialization
	//----------------------------------------------------------------------------------------------
	public void init(String _transformation, String _algorithm) {
		this.transformation = _transformation;
		this.algorithm = _algorithm;
	}
	
	public void initPasswordEncryptor() {
		passwordEncryptor = new BasicPasswordEncryptor();
	}
	
/*	
 	private String secPass = null;
	private BasicTextEncryptor basicTextEncryptor = null;
  
  public String encryptText(String text) {
		//StandardPBEStringEncryptor sbe = new StandardPBEStringEncryptor();
		StandardPBEStringEncryptor sbe = new StandardPBEStringEncryptor();
		sbe.setAlgorithm("PBEWithMD5AndDES");
		sbe.setPassword(this.secPass);
		return sbe.encrypt(text);
		//return basicTextEncryptor.encrypt(text);
	}
	
	public String decryptText(String text) {
		//StandardPBEStringEncryptor sbe = new StandardPBEStringEncryptor();
		StandardPBEStringEncryptor sbe = new StandardPBEStringEncryptor();
		sbe.setAlgorithm("PBEWithMD5AndDES");
		sbe.setPassword(this.secPass);
		return sbe.decrypt(text);
		//return basicTextEncryptor.decrypt(text);
	}
	
	public void initBasicTextEncryptor(String password) {
		if (basicTextEncryptor == null) {
			basicTextEncryptor = new BasicTextEncryptor();
			basicTextEncryptor.setPassword(password);
			
		}
	}
	

	
	public void initSecPass(String password) {
		this.secPass = password;
	}
	
	public String getSecPass() {
		return secPass;
	}*/
	

}
